package com.uroad.jilinpda.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.uroad.jilinpda.R;
import com.uroad.jilinpda.common.BaseActivity;
import com.uroad.jilinpda.modle.ETCDealInfoMDL;
import com.uroad.jilinpda.util.QRCodeUtil;
import com.uroad.jilinpda.webservice.ETCHandleWS;
import com.uroad.jilinpda.widget.SYSDiaLogUtils;
import com.uroad.lib.json.FastJsonUtils;
import com.uroad.lib.widget.dialog.DialogHelper;


import butterknife.BindView;
import butterknife.ButterKnife;

import static com.uroad.jilinpda.webservice.BaseWS.getNewUrl;

public class SigningQRCodeActivity extends BaseActivity {

    @BindView(R.id.tv_platNumber)
    TextView tvPlatNumber;
    @BindView(R.id.ivQRCode)
    ImageView ivQRCode;
    private String codeUrl="";
    private String plateNumber="";
    private String orderno="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentLayout(R.layout.activity_signing_qrcode);
        setTitle("绑定银行卡");
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        codeUrl=ETCDealInfoMDL.getInstance().getCodeUrl();
        plateNumber= ETCDealInfoMDL.getInstance().getPlat();
        Bundle bundle = getIntent().getExtras();
        if (bundle!=null){
            orderno=bundle.getString("orderno");
            plateNumber=bundle.getString("plateNumber");
        }
        if (TextUtils.isEmpty(orderno)) {
            if (TextUtils.isEmpty(codeUrl)) {
                showShortToast("获取的二维码错误");
            } else {
                Bitmap bmpQRCode = QRCodeUtil.createQRImage(codeUrl, 400, 400);
                ivQRCode.setImageBitmap(bmpQRCode);
            }
        }else {
            doCodeWork();
        }
        if (!TextUtils.isEmpty(plateNumber)){
            tvPlatNumber.setText(plateNumber);
        }
    }

    private void doCodeWork() {
        DialogHelper.showLoading(this, "正在获取签约二维码...");
       doRequest(ETCHandleWS.bankcodestate, ETCHandleWS.bankcodestate(orderno), ETCHandleWS.bankcodestate);

    }
    @Override
    public void OnHttpTaskComplete(String result, String type) {
        super.OnHttpTaskComplete(result, type);
        DialogHelper.endLoading();
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (ETCHandleWS.bankcodestate.equalsIgnoreCase(type)){
            if (FastJsonUtils.GetJsonStatu1(jsonObject)) {
                JSONObject data = jsonObject.getJSONObject("data");
                String url = data.getString("url");
                if (TextUtils.isEmpty(url)) {
                    showShortToast("获取的二维码错误");
                } else {
                    Bitmap bmpQRCode = QRCodeUtil.createQRImage(url, 400, 400);
                    ivQRCode.setImageBitmap(bmpQRCode);
                }
            }else {
                SYSDiaLogUtils.showConfirmDialogType1(this, "温馨提示", jsonObject.getString("msg"),"我知道了", new SYSDiaLogUtils.ConfirmDialogListener() {
                    @Override
                    public void onClickButton(boolean clickLeft, boolean clickRight) {
                        if (clickLeft) {

                        } else if (clickRight) {

                        }
                        finish();
                    }
                });
            }
        }
    }
}
